$.extend({
    CommonAdmin: {
        //////////////////////////////////////////////
        //初始化页面
        //////////////////////////////////////////////
        PageInit: function () {
            //Bootstrap
            $.Common.Bootstrap.PageInit();
            //权限检查
            // this.Permission.CheckElement();
            //绑定数据项
            // this.DataBind.fnBind();
            //绑定数据项(通用数据)
            // $.Common.DataBind.fnBind();
            //打开相关窗口
            $(document).delegate('[OpenWindow]', 'click', function (e) {
                $.CommonAdmin.OpenWindow({
                    ID: $(this).attr('OpenWindow'),
                    Element: this,
                    Event: e
                });
            });
            //初始化页面
            $.Common.PageInit();
        },
        //////////////////////////////////////////////
        //图片放大缩小
        //////////////////////////////////////////////
        imgToSize: function (size, id) {
            var img = $("#" + id);
            var oWidth = img.width(); //取得图片的实际宽度
            var oHeight = img.height(); //取得图片的实际高度
            if (oHeight + size > 0) {
                img.width(oWidth + size);
                img.height(oHeight + size / oWidth * oHeight);
            } else {
                $.KOC.Tips.Open("", "宽度和高度最小无法缩放了", {Timeout: 2000});
            }

        },
        //////////////////////////////////////////////
        //打开相关窗口
        //////////////////////////////////////////////
        OpenWindow: function (parm, onEnter, onClose) {
            var _parm = {
                Info: false,
                ID: null,
                Type: null,
                URL: '',
                Parm: '',
                RightCode: null,
                Modal: true,
                Element: null,
                Event: null,
                onEnter: onEnter,
                onClose: onClose
            };
            switch (typeof parm) {
                case 'string':
                    _parm.ID = parm;
                    break;
                case 'object':
                    _parm.ID = parm.ID;
                    _parm.Element = parm.Element;
                    _parm.Event = parm.Event;
                    break;
                default:
                    $.KOC.Tips.Open('Error', '传入参数错误', {Timeout: 1000});
                    return;
            }
            if (!_parm.ID) {
                $.KOC.Tips.Open('Error', '传入参数错误', {Timeout: 1000});
                return;
            }
            if (_parm.ID.indexOf('[Info]') == 0) {
                _parm.Info = true;
                _parm.ID = _parm.ID.substr(6);
            }
            var _parmValue = _parm.ID.split('|');
            for (var i = 0; i < _parmValue.length; i++) {
                var ThisValue = _parmValue[i].split(':');
                switch (ThisValue.length) {
                    case 1:
                        _parm.Type = ThisValue[0].substr(6, 2);
                        _parm.ID = ThisValue[0];
                        break;
                    case 2:
                        if (_parm.Parm) {
                            _parm.Parm += '&';
                        }
                        _parm.Parm += ThisValue[0] + '=' + ThisValue[1];
                        break;
                    case 3:
                        _parm.Type = ThisValue[1];
                        _parm.ID = ThisValue[2];
                        break;
                }
            }
            // console.log(_parm.Parm);
            switch (_parm.Type) {
                case 'UserAvatar'://用户头像
                    _parm.URL = {
                        overlayClose: true,
                        href: '/user-manage/info-manage/avatar/' + _parm.ID,
                        innerWidth: 700,
                        innerHeight: 492
                    };
                    _parm.RightCode = '{User}[Info](Avatar)';
                    break;
                case 'UserCover'://用户封面
                    _parm.URL = {
                        overlayClose: true,
                        href: '/user-manage/info-manage/cover/' + _parm.ID,
                        innerWidth: 700,
                        innerHeight: 492
                    };
                    _parm.RightCode = '{User}[Info](Cover)';
                    break;
                case 'UserAnchorInfo'://艺人信息
                    _parm.URL = '/user-manage/anchor-manage/info/' + _parm.ID;
                    _parm.RightCode = '{User}[Anchor](Info)';
                    break;
                case 'UserAnchorEdit'://艺人编辑
                    _parm.URL = '/user-manage/anchor-manage/edit/' + _parm.ID;
                    _parm.RightCode = '{User}[Anchor](Edit)';
                    break;
                case 'UserAnchorTurnFamily'://艺人转工会
                    _parm.URL = '/user-manage/anchor-manage/turn-family/' + _parm.ID;
                    _parm.RightCode = '{User}[Anchor](Turn-Family)';
                    break;
                case 'UserAnchorApplyEdit'://艺人申请审批
                    _parm.URL = '/user-manage/anchor-manage/apply-edit/' + _parm.ID;
                    _parm.RightCode = '{User}[Anchor](Apply-Edit)';
                    break;
                case 'UserAnchorImgEdit'://艺人封面审核
                    _parm.URL = '/user-manage/anchor-manage/img-edit/' + _parm.ID;
                    _parm.RightCode = '{User}[Anchor](ImgEdit)';
                    break;
                case 'UserAddGrant'://用户信息
                    _parm.URL = '/user-manage/info-manage/addgrant/' + _parm.ID;
                    _parm.RightCode = '{User}[Info](AddGrant)';
                    break;
                case 'UserInfoInfo'://用户信息
                    _parm.URL = '/user-manage/info-manage/info/' + _parm.ID;
                    _parm.RightCode = '{User}[Info](Info)';
                    break;
                case 'UserInfoEdit'://用户编辑
                    _parm.URL = '/user-manage/info-manage/edit/' + _parm.ID;
                    _parm.RightCode = '{User}[Info](Edit)';
                    break;
                case 'UserInfoAvatarImgEdit'://用户头像编辑
                    _parm.URL = '/user-manage/info-manage/avatar-edit/' + _parm.ID;
                    _parm.RightCode = '{User}[Info](Avatar-Edit)';
                    break;
                case 'UserAddSubtract'://用户加币
                    _parm.URL = '/user-manage/info-manage/add-subtract/' + _parm.ID;
                    _parm.RightCode = '{User}[Info](Add-Subtract)';
                    break;
                case 'UserFamilyInfo'://公会信息
                    _parm.URL = '/user-manage/family-manage/info/' + _parm.ID;
                    _parm.RightCode = '{User}[Family](Info)';
                    break;
                case 'UserFamilyEdit'://公会编辑
                    _parm.URL = '/user-manage/family-manage/edit/' + _parm.ID;
                    _parm.RightCode = '{User}[Family](Edit)';
                    break;
                case 'FamilyAnchorInfo'://公会组播
                    _parm.URL = '/user-manage/family-manage/anchor/' + _parm.ID;
                    _parm.RightCode = '{User}[Family](Anchor)';
                    break;
                case 'UserFamilyApplyEdit'://公会申请审批
                    _parm.URL = '/user-manage/family-manage/apply-edit/' + _parm.ID;
                    _parm.RightCode = '{User}[Family](Apply-Edit)';
                    break;
                case 'RoomFreezeMelt'://房间解冻/冻结
                    _parm.URL = '/room-manage/info-manage/freeze-melt/' + _parm.ID;
                    _parm.RightCode = '{Room}[Info](Freeze-Melt)';
                    break;
                // case 'StatisticsChannelInfo'://渠道详情
                //     _parm.URL = '/statistics-manage/channel-manage/info/' + _parm.ID;
                //     _parm.RightCode = '{Statistics}[Channel](Info)';
                //     break;
                case 'StatisticsInviteInfo'://邀请详情
                    _parm.URL = '/statistics-manage/invite-manage/info/' + _parm.ID;
                    _parm.RightCode = '{Statistics}[Invite](Info)';
                    break;
                case 'FinanceAnchorCashEdit'://艺人结算
                    _parm.URL = '/finance-manage/anchor-manage/cash-edit/' + _parm.ID;
                    _parm.RightCode = '{Finance}[Anchor](Cash-Edit)';
                    break;
                case 'FinanceAnchorBaseEdit'://艺人基本工资结算
                    _parm.URL = '/finance-manage/anchor-manage/base-edit/' + _parm.ID;
                    _parm.RightCode = '{Finance}[Anchor](Base-Edit)';
                    break;
                case 'FinanceFamilyCashEdit'://公会结算
                    _parm.URL = '/finance-manage/family-manage/cash-edit/' + _parm.ID;
                    _parm.RightCode = '{Finance}[Family](Cash-Edit)';
                    break;
                case 'ManagerAdd'://管理员添加
                    _parm.URL = '/system-manage/manager-manage/add' + (_parm.ID ? '/' + _parm.ID : '');
                    _parm.RightCode = '{System}[Manager](Add)';
                    break;
                case 'ManagerEdit'://管理员编辑
                    _parm.URL = '/system-manage/manager-manage/edit' + (_parm.ID ? '/' + _parm.ID : '');
                    _parm.RightCode = '{System}[Manager](edit)';
                    break;
                default:
                    $.KOC.Tips.Open('Error', '未知的打开类型', {Timeout: 1000});
                    return;
            }
            // if (_parm.RightCode != undefined) {
            //     if (!_parm.RightCode && !_StateData.IsLogin) {
            //         $.CommonMain.Login('In');
            //         return;
            //     }
            //     if ($.Common.Permission.Check(_parm.RightCode) != 0) {
            //         if (_parm.Info) {
            //             try {
            //                 parent.$.colorbox.close();
            //             } catch (ex) {
            //                 window.close();
            //             }
            //         } else {
            //             $.KOC.Tips.Open('Error', '您没有进行此操作的权限', {Timeout: 1000});
            //         }
            //         return;
            //     }
            // }
            if (!_parm.URL) {
                return;
            }
            if (_parm.Parm) {
                _parm.URL += (_parm.URL.indexOf('?') >= 0 ? '&' : '?') + _parm.Parm;
            }
            if (_parm.Info) {
                window.location.href = _parm.URL;
                return;
            }
            if (_parm.Modal) {
                $.Common.ModalWindow.Open(_parm.URL, _parm.onEnter, _parm.onClose);
                return;
            }
            if ((_parm.Element ? _parm.Element.nodeName : null) === 'A') {
                $(_parm.Element).attr('href', _parm.URL);
                if (_parm.Event) {
                    $.Common.DefaultPrevented.Call(_parm.Event, _parm.Element);
                }
            } else {
                window.location.href = _parm.URL;
            }
        },
        //////////////////////////////////////////////
        //退出登录
        //////////////////////////////////////////////
        Logout: function (req) {
            $.getJSON("/ajax/base.logout"
                , {}
                , function (json) {
                    if (json.flag !== 0) {
                        $.KOC.Tips.Open('Error', json.msg + "(" + json.flag + ")", {Timeout: 1000});
                        return;
                    }
                    $.KOC.Tips.Open('Success', '安全退出完成，本页将在1秒后自动刷新', {
                        Timeout: 1000,
                        onClose: function () {
                            if (req) {
                                $.Common.ModalWindow.fnEnter();
                            }
                            location.reload(true);
                        }
                    });
                });
        },
        //////////////////////////////////////////////
        //权限管理
        //////////////////////////////////////////////
        Permission: {
            Check: function (code) {
                //return false;
                try {
                    _StateData;
                } catch (ex) {
                    return false;
                }
                if (!_StateData.IsLogin) {
                    //未登录
                    return false;
                }
                if (_StateData.Admin.IsMaster === 1) {
                    return true;
                }
                if (!code || $.trim(code) == '') {
                    return true;
                }
                var _code = _.compact(code.split('|'));
                for (var i in _code) {
                    code = $.trim(_code[i]);
                    if (code != '' && _StateData.Admin.RightCode.indexOf(code) >= 0) {
                        return true
                    }
                }
                return false;
            },
            CheckElement: function (element) {
                if (!element) {
                    element = $('body');
                }
                if (!(element instanceof jQuery)) {
                    element = $(element);
                }
                if (element.attr('RightCode') && !$.CommonAdmin.Permission.Check($(this).attr('RightCode'))) {
                    if ($(this).attr('RightDisabled')) {
                        $(this).addClass('disabled').attr('disabled', 'disabled');
                    } else {
                        $(this).remove();
                        return;
                    }
                }
                $(element.find('[RightCode]')).each(function () {
                    if (!$.CommonAdmin.Permission.Check($(this).attr('RightCode'))) {
                        if ($(this).attr('RightDisabled')) {
                            $(this).addClass('disabled').attr('disabled', 'disabled');
                        } else {
                            $(this).remove();
                        }
                    }
                });
                //if (!element) {
                //    element = $('body');
                //}
                //if (!(element instanceof jQuery)) {
                //    element = $(element);
                //}
                //if (element.attr('RightCode') && !$.CommonAdmin.Permission.Check($(this).attr('RightCode'))) {
                //    if ($(this).attr('RightDisabled')) {
                //        $(this).addClass('disabled').attr('disabled', 'disabled');
                //    } else {
                //        $(this).remove();
                //        return;
                //    }
                //}
                //$(element.find('[RightCode]')).each(function () {
                //    if (!$.CommonAdmin.Permission.Check($(this).attr('RightCode'))) {
                //        if ($(this).attr('RightDisabled')) {
                //            $(this).addClass('disabled').attr('disabled', 'disabled');
                //        } else {
                //            $(this).remove();
                //        }
                //    }
                //});
            }
        }
    }
});